//
//  ByUserChangePhoneVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/11.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByUserChangePhoneVC.h"

@interface ByUserChangePhoneVC ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, assign) NSInteger countTime;
@property (nonatomic, strong) UIButton *button;

@end

@implementation ByUserChangePhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    self.title = @"修改手机号";
    
    [self initViews];
    // Do any additional setup after loading the view.
}


- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    NSArray *places = @[@"原手机号码",@"新手机号码",@"验证码"];
    CGFloat margin = 10;
    CGFloat width = [@"原手机号码" getSizeOfString:15].width + 10;
    for (int i = 0; i < places.count; i ++) {
        UITextField *tf = [[UITextField alloc] init];
        [self.view addSubview:tf];
        [tf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.top.equalTo(@(margin + 50 * i + i * 1));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
        }];
        tf.placeholder = places[i];
        tf.font = FONT(15);
        tf.tag = 10 + i;
        tf.backgroundColor = [UIColor whiteColor];
        tf.textColor = [UIColor blackColor];
        [tf setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        tf.keyboardType = UIKeyboardTypeNumberPad;
        tf.delegate = self;
        if (i == 0) {
            tf.text = BY_USER.mobile;
        }
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width + 30, 50)];
        UILabel *titleLabel = [[UILabel alloc] init];
        [leftView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftView).offset(10);
            make.top.equalTo(leftView).offset(0);
            make.bottom.equalTo(leftView).offset(0);
            make.right.equalTo(leftView).offset(0);
          
        }];
        
        titleLabel.font = FONT(15);
        titleLabel.textColor = BY_TEXT_COLOR;
        titleLabel.text = places[i];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.backgroundColor = [UIColor whiteColor];
        
        tf.leftView = leftView;
        tf.leftViewMode = UITextFieldViewModeAlways;
        if (i == 2) {
           
            UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width + 30, 50)];
            tf.rightView = rightView;
            tf.rightViewMode = UITextFieldViewModeAlways;
            
            UILabel *line = [[UILabel alloc] init];
            [rightView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(rightView).offset(0);
                make.centerY.equalTo(rightView).offset(0);
                make.size.mas_equalTo(CGSizeMake(1, 20));
            }];
            line.backgroundColor = BY_LINE_COLOR;
            
            
            
            UIButton *codeButton = [[UIButton alloc] init];
            [rightView addSubview:codeButton];
            [codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(rightView).offset(0);
                make.left.equalTo(line.mas_right).offset(0);
                make.top.equalTo(rightView).offset(0);
                make.bottom.equalTo(rightView).offset(0);
            }];
            [codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            [codeButton setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
            codeButton.titleLabel.font = FONT(12);
            [codeButton addTarget:self action:@selector(sendCodeAction:) forControlEvents:UIControlEventTouchUpInside];
            self.codeBtn = codeButton;
        }
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(50);
        make.right.equalTo(self.view).offset(-50);
        make.top.equalTo(self.view).offset(50 * 3 + 100);
        make.height.mas_equalTo(@50);
    }];
    button.backgroundColor = rgba(137, 137, 137, 1);
    [button setTitle:@"确认" forState:UIControlStateNormal];
    button.titleLabel.font = FONT(16);
    button.userInteractionEnabled = NO;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button = button;
}

- (void)btnAction:(UIButton *)btn{
    UITextField *mobileTF = [self.view viewWithTag:11];
    UITextField *codeTF = [self.view viewWithTag:12];
    
    if (mobileTF.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号码"];
        return;
    }
    
    if (mobileTF.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确的手机号码"];
        return;
    }
    if (codeTF.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入验证码"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = mobileTF.text;
    model.code = codeTF.text;
    [ByApiUser userChangeMobile:model success:^(ByNetBaseResult *result) {
        
        NSLog(@"换绑手机号 = %@", result.data);
        [self leftItemsAction:0];
        ByModelUser *user = BY_USER;
        user.mobile = mobileTF.text;
        [ByCacheUser setUserInfo:user];
        [ByCoreToolAlert showSuccessWithText:@"绑定成功"];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}


#pragma mark - 发送验证码
- (void)sendCodeAction:(UIButton *) button{
    UITextField *textField = [self.view viewWithTag:11];
    if (textField.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号码"];
        return;
    }
    
    if (textField.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确的手机号码"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = textField.text;
    model.type = @"6";
    [ByApiUser smsSendCode:model success:^(ByNetBaseResult *result){
        
        __weak typeof(self) weakSelf = self;
        NSLog(@"验证码 = %@", result.data);
        [ByCoreToolAlert showText:[NSString stringWithFormat:@"%@",result.data[@"data"]] state:ByAlertStateModeSuccess duration:2];
        weakSelf.countTime = 60;
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdownAction:) userInfo:nil repeats:YES];
        [timer fire];
        
    } failure:^(ByNetBaseResult *result){
        
        button.enabled = YES;
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)countdownAction:(NSTimer *)timer{
    if (self.countTime > 0){
        
        self.countTime --;
        [_codeBtn setTitle:[NSString stringWithFormat:@"(%ld)重新获取", (long)self.countTime] forState:UIControlStateNormal];
        
    }else {
        
        _codeBtn.enabled = YES;
        [timer invalidate];
        [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    UITextField *oldMobileTF = [self.view viewWithTag:10];
    UITextField *mobileTF = [self.view viewWithTag:11];
    UITextField *codeTF = [self.view viewWithTag:12];
    
    if (oldMobileTF.text.length > 0 && mobileTF.text.length > 0 && codeTF.text.length > 0) {
        self.button.backgroundColor = BY_THEME_COLOR;
        self.button.userInteractionEnabled = YES;
    }else{
        self.button.backgroundColor = rgba(137, 137, 137, 1);
        self.button.userInteractionEnabled = NO;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
